﻿using System;
using System.Web.Routing;


namespace Wayloop.Web.Mvc.Routing
{
    public class LowercaseRoute : Route
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LowercaseRoute"/> class.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="routeHandler">The route handler.</param>
        public LowercaseRoute(string url, IRouteHandler routeHandler) : base(url, routeHandler)
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="LowercaseRoute"/> class.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="defaults">The defaults.</param>
        /// <param name="routeHandler">The route handler.</param>
        public LowercaseRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) : base(url, defaults, routeHandler)
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="LowercaseRoute"/> class.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="defaults">The defaults.</param>
        /// <param name="constraints">The constraints.</param>
        /// <param name="routeHandler">The route handler.</param>
        public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler) : base(url, defaults, constraints, routeHandler)
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="LowercaseRoute"/> class.
        /// </summary>
        /// <param name="url">The URL pattern for the route.</param>
        /// <param name="defaults">The values to use if the URL does not contain all the parameters.</param>
        /// <param name="constraints">A regular expression that specifies valid values for a URL parameter.</param>
        /// <param name="dataTokens">Custom values that are passed to the route handler, but which are not used to determine whether the route matches a specific URL pattern. The route handler might need these values to process the request.</param>
        /// <param name="routeHandler">The object that processes requests for the route.</param>
        public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler) : base(url, defaults, constraints, dataTokens, routeHandler)
        {
        }


        /// <summary>
        /// Returns information about the URL that is associated with the route.
        /// </summary>
        /// <param name="requestContext">An object that encapsulates information about the requested route.</param>
        /// <param name="values">An object that contains the parameters for a route.</param>
        /// <returns>
        /// An object that contains information about the URL that is associated with the route.
        /// </returns>
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            VirtualPathData virtualPathData = base.GetVirtualPath(requestContext, values);

            if (virtualPathData != null) {
                string virtualPath = virtualPathData.VirtualPath;
                int queryStartIndex = virtualPath.IndexOf('?');

                if (queryStartIndex > 0) {
                    string path = virtualPath.Substring(0, queryStartIndex);
                    string query = virtualPath.Substring(queryStartIndex);

                    virtualPath = String.Concat(path.ToLowerInvariant(), query);
                }
                else {
                    virtualPath = virtualPath.ToLowerInvariant();
                }

                virtualPathData.VirtualPath = virtualPath;
            }

            return virtualPathData;
        }
    }
}